package com.atguigu.educms.controller;


import com.atguigu.commonutils.R;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.atguigu.educms.vo.BannerVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author 李川
 * @since 2020-05-17
 */
@RestController
//@CrossOrigin
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;
    //1 分页查询banner
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page,@PathVariable long limit) {
        Page<CrmBanner> pageBanner = new Page<>(page,limit);
        bannerService.page(pageBanner,null);
        return R.ok().data("items",pageBanner.getRecords()).data("total",pageBanner.getTotal());
    }

    //2 添加banner
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        bannerService.save(crmBanner);
        return R.ok();
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public R get(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("item", banner);
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner crmBanner) {
        bannerService.updateById(crmBanner);
        return R.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        bannerService.removeById(id);
        return R.ok();
    }

    //带条件的分页查询
    @ApiOperation(value = "带条件分页查询首图")
    @PostMapping("pageBannerCondition/{current}/{limit}")
    public  R pageBannerCondition(
            @ApiParam(name="current",value = "当前页",required = true)
            @PathVariable long current,
            @ApiParam(name="limit",value = "页面数大小",required = true)
            @PathVariable long limit,
            @ApiParam(name="bannerVo",value = "分页查询条件",required = true)
            @RequestBody(required = false) BannerVo bannerVo){  //参数值可以没有但是要post请求
        //创建page对象
        Page<CrmBanner> bannerPage=new Page<>(current,limit);
        //构建条件
        QueryWrapper<CrmBanner> wrapper =new QueryWrapper<>();
        //多条件组合查询  ---动态sql
        String title=bannerVo.getTitle();
        String linkUrl=bannerVo.getLinkUrl();
        String begin=bannerVo.getBegin();
        String end=bannerVo.getEnd();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(linkUrl)){
            wrapper.like("link_url",linkUrl);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        //条件查询分页对象
        bannerService.page(bannerPage,wrapper);
        long total=bannerPage.getTotal(); //总记录数
        List<CrmBanner> records=bannerPage.getRecords(); //数据集合
        Map map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);  //链式编程也可以
    }
 }

